package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.SysUserPowerBo;
import com.ruoyi.system.domain.vo.SysUserPowerVo;
import com.ruoyi.system.domain.SysUserPower;
import com.ruoyi.system.mapper.SysUserPowerMapper;
import com.ruoyi.system.service.ISysUserPowerService;

import java.util.List;
import java.util.Collection;

/**
 * 用户关联项目Service业务层处理
 *
 * @author zxs
 * @date 2024-10-10
 */
@RequiredArgsConstructor
@Service
public class SysUserPowerServiceImpl implements ISysUserPowerService {

    private final SysUserPowerMapper baseMapper;

    /**
     * 查询用户关联项目
     */
    @Override
    public SysUserPowerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户关联项目列表
     */
    @Override
    public TableDataInfo<SysUserPowerVo> queryPageList(SysUserPowerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUserPower> lqw = buildQueryWrapper(bo);
        Page<SysUserPowerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户关联项目列表
     */
    @Override
    public List<SysUserPowerVo> queryList(SysUserPowerBo bo) {
        LambdaQueryWrapper<SysUserPower> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysUserPower> buildQueryWrapper(SysUserPowerBo bo) {
//        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysUserPower> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SysUserPower::getUserId, bo.getUserId());
        lqw.eq(bo.getPowerId() != null, SysUserPower::getPowerId, bo.getPowerId());
        return lqw;
    }

    /**
     * 新增用户关联项目
     */
    @Override
    public Boolean insertByBo(SysUserPowerBo bo) {
        SysUserPower add = BeanUtil.toBean(bo, SysUserPower.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户关联项目
     */
    @Override
    public Boolean updateByBo(SysUserPowerBo bo) {
        SysUserPower update = BeanUtil.toBean(bo, SysUserPower.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysUserPower entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户关联项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
